package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class myCorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        //new urlBasedCorsConfigurationSource这个实现类
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);

        //用urlBasedCorsConfigurationSource去注册跨域配置  path ,url
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        //返回这个需要CorsConfigurationSource ，但是发现它是个接口，然后去找urlBasedCorsConfigurationSource这个实现类
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}
